﻿using System;
using System.Collections.Generic;
using System.Management.Automation.Host;
using System.Management.Automation;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace PSSymbolStore.Manager
{
    public class TransactionManagerHostUserInterface : PSHostUserInterface
    {
        private readonly ITransactionManagerView _view;

        public TransactionManagerHostUserInterface(ITransactionManagerView view)
        {
            _view = view;
        }

        public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
        {
            throw new NotImplementedException();
        }

        public override int PromptForChoice(string caption, string message, Collection<System.Management.Automation.Host.ChoiceDescription> choices, int defaultChoice)
        {
            // I don't support the "Suspend" choice, if it exists - so scrub it from the list.

            var newChoices = new Collection<ChoiceDescription>();

            foreach (System.Management.Automation.Host.ChoiceDescription choice in choices)
            {
                if (!choice.Label.Contains("Suspend"))
                {
                    newChoices.Add(ChoiceDescription.FromPSChoiceDescription(choice));
                }
            }

            return _view.Prompt(caption, message, newChoices, defaultChoice);
        }

        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
        {
            throw new NotImplementedException();
        }

        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
        {
            throw new NotImplementedException();
        }

        public override PSHostRawUserInterface RawUI
        {
            get { return null; }
        }

        public override string ReadLine()
        {
            throw new NotImplementedException();
        }

        public override System.Security.SecureString ReadLineAsSecureString()
        {
            throw new NotImplementedException();
        }

        public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
        {
            throw new NotImplementedException();
        }

        public override void Write(string value)
        {
            throw new NotImplementedException();
        }

        public override void WriteDebugLine(string message)
        {
            Debug.WriteLine(message);
            _view.WriteOutput(message, OutputMessageType.Debug);
        }

        public override void WriteErrorLine(string value)
        {
            _view.WriteOutput(value, OutputMessageType.Error);
        }

        public override void WriteLine(string value)
        {
            _view.WriteOutput(value, OutputMessageType.Normal);
        }

        public override void WriteProgress(long sourceId, ProgressRecord record)
        {
            _view.ReportProgress((record.RecordType == ProgressRecordType.Processing),
                record.PercentComplete, record.CurrentOperation, record.StatusDescription);
        }

        public override void WriteVerboseLine(string message)
        {
            _view.WriteOutput(message, OutputMessageType.Verbose);
        }

        public override void WriteWarningLine(string message)
        {
            _view.WriteOutput(message, OutputMessageType.Warning);
        }
    }
}
